'use strict';

/* Controllers */

angular.module('myApp.controllersLogin', []).
        controller('LoginCtrl', ['$scope', '$location',
                                'serviceStorage',
                                'serviceGame', 'serviceUser',
            function($scope, location, storage, game, user) {
                
                $scope.user = {
                    email : storage.getItem('email'),
                    password : storage.getItem('password')
                };
                

                function validate(user) {
                    console.log(user);
                    if (typeof user.email !== 'undefined' && typeof user.password !== 'undefined')
                    {
                        return true;
                    }
                    return false;
                }

                $scope.submit = function() {
                    if (validate($scope.user)) {
                        user.login(
                            $scope.user.email,
                            CryptoJS.MD5($scope.user.password).toString(),
                            function () {
                                console.log('function 1');
                                storage.setItem('email', $scope.user.email);
                                storage.setItem('password', $scope.user.password);
                                game.clear();
                                location.path('/game/players');
                                $scope.$apply();
                                console.log('redirect');
                            },
                            function(error) {
                                $('#popupLoginIncorrect').modal('show');
                            },
                            function (data) {
                                console.log('function 2');
                                console.log(data);
                                $('#globalInvitationEmail').html(data.email);
                                $('#globalInvitationOkBtn').click(function() {

                                    game.ownerUser = data;
                                    console.log(game);
                                    location.path('/game/play/join');
                                    $scope.$apply();
                                    console.log('redirect');
                                    $('#globalInvitation').modal('hide');
                                });
                                $('#globalInvitation').modal('show');
                            }
                        );
                    };
                };
            }
        ])
        .controller('LogoutCtrl',   ['$scope', '$location',
                                    'serviceGame', 'serviceUser',
            function($scope, location, game, user) {
                user.clear();
                game.clear();
                location.path('/login');
                console.log('redirect');
            }
        ])
        .controller('CreateAccountCtrl', ['$scope', '$location', 'serviceStorage', 'serviceUser',
            function($scope, location, storage, user) {

                $scope.user = {
                    email: '',
                    password: '',
                    password2: ''
                };

                function validate(user) {
                    if (user.email != '' && user.password == user.password2
                            && typeof user.password !== 'undefined'
                            && user.password != '')
                    {
                        return true;
                    }
                    return false;
                }

                $scope.submit = function() {
                    if (validate($scope.user)) {
                        user.createAccount(
                            $scope.user.email,
                            CryptoJS.MD5($scope.user.password).toString(),
                            function () {
                                storage.setItem('email', $scope.user.email);
                                storage.setItem('password', $scope.user.password);
                                location.path('/game/players');
                                $scope.$apply();
                            },
                            function (error) {
                                $('#popupAccountExist').modal('show');
                            }
                        );
                    };
                };
            }
        ]);